package handlers

import (
	"ticket/internal/db"
	"ticket/internal/db/model"
	"ticket/internal/schemas"

	"github.com/gin-gonic/gin"
)

type RoleHandler struct{}

func RegisterRoleRouter(group *gin.RouterGroup) {
	service := &RoleHandler{}
	group.POST("/create", service.Create)
	group.GET("/list", service.List)
}

// Create godoc
// @Summary 创建角色
// @Description 创建角色
// @Tags 角色
// @ID /role/create
// @Accept json
// @Produce json
// @Param data body schemas.RoleCreate true "角色信息"
// @Success 200 {object} schemas.Response{data=string} "成功"
// @Failure 400 {object} schemas.Response{data=string} "请求错误"
// @Failure 500 {object} schemas.Response{data=string} "内部错误"
// @Router /role/create [post]
func (h *RoleHandler) Create(ctx *gin.Context) {
	params := &schemas.RoleCreate{}
	if err := params.BindValidParam(ctx); err != nil {
		schemas.ResponseError(ctx, schemas.RoleParamInvalid, err)
		return
	}

	role := &model.Role{
		ID:   params.ID,
		Name: params.Name,
	}
	if err := role.Create(db.GormDB); err != nil {
		schemas.ResponseError(ctx, schemas.RoleCreateFailed, err)
		return
	}
	schemas.ResponseSuccess(ctx, nil)
}

// List godoc
// @Summary 获取角色列表
// @Description 获取角色列表
// @Tags 角色
// @ID /role/list
// @Success 200 {object} schemas.Response{data=[]model.Role} "成功"
// @Failure 400 {object} schemas.Response{data=string} "请求错误"
// @Failure 500 {object} schemas.Response{data=string} "内部错误"
// @Router /role/list [get]
func (h *RoleHandler) List(ctx *gin.Context) {
	role := &model.Role{}
	roles, err := role.List(db.GormDB, 1, 10)
	if err != nil {
		schemas.ResponseError(ctx, schemas.RoleListFailed, err)
		return
	}
	schemas.ResponseSuccess(ctx, roles)
}
